package hust.ioic.oa.view.action;

import hust.ioic.oa.base.BaseAction;
import hust.ioic.oa.domain.KnowType;

import hust.ioic.oa.domain.Role;
import hust.ioic.oa.utils.KnowTypeUtils;

import java.util.ArrayList;
import java.util.List;

import org.jboss.weld.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class KnowTypeAction extends BaseAction<KnowType> {

	private Integer parentId;

	/**
	 * 列表
	 * 
	 * @return
	 */
	public String list() {
		List<KnowType> knowTypeList = null;
		if (parentId == null) {
			knowTypeList = knowTypeService.findTopList();
		} else {
			knowTypeList = knowTypeService.findChildren(parentId);
			KnowType parent = knowTypeService.getById(parentId);
			ActionContext.getContext().put("parent", parent);
		}
		ActionContext.getContext().put("knowTypeList", knowTypeList);
		return "list";
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		knowTypeService.delete(model.getId());

		return "toList";
	}

	/**
	 * 更改页面
	 * 
	 * @return
	 */
	public String editUI() {
		KnowType knowType = knowTypeService.getById(model.getId());
		List<String> ktNames = new ArrayList<String>();
		List<KnowType> roleList = knowTypeService.findAll();
		for (KnowType role : roleList) {
			if (role != null && role.getId() != model.getId()) {
				ktNames.add(role.getName());
			}
		}
		ActionContext.getContext().put("ktNames", ktNames);
		List<KnowType> topList = knowTypeService.findTopList();
		List<KnowType> knowTypeList = KnowTypeUtils.getAllKnowTypes(topList);
		ActionContext.getContext().put("knowTypeList", knowTypeList);

		// 准备回显的数据

		ActionContext.getContext().getValueStack().push(knowType);
		if (knowType.getParent() != null) {
			parentId = knowType.getParent().getId();
		}
		return "saveUI";
	}

	/**
	 * 更改
	 * 
	 * @return
	 */
	public String edit() {
		KnowType knowType = knowTypeService.getById(model.getId());
		KnowType parent = knowTypeService.getById(parentId);
		// 2，设置要修改的属性
		knowType.setName(model.getName());
		knowType.setRemark(model.getRemark());
		knowType.setParent(parent); // 设置所属的上级部门

		// 3，更新到数据库中
		knowTypeService.update(knowType);
		return "toList";
	}

	/**
	 * 添加页面
	 * 
	 * @return
	 */
	public String addUI() {
		List<String> ktNames = new ArrayList<String>();
		List<KnowType> roleList = knowTypeService.findAll();
		for (KnowType role : roleList) {
			if (role != null) {
				ktNames.add(role.getName());
			}
		}
		ActionContext.getContext().put("ktNames", ktNames);
		List<KnowType> topList = knowTypeService.findTopList();
		List<KnowType> knowTypeList = KnowTypeUtils.getAllKnowTypes(topList);
		ActionContext.getContext().put("knowTypeList", knowTypeList);
		return "saveUI";
	}

	/**
	 * 添加
	 * 
	 * @return
	 */
	public String add() {
		KnowType parent = knowTypeService.getById(parentId);
		model.setParent(parent);
		model.setFileNum(0);
		knowTypeService.save(model);
		return "toList";
	}

	// ====================Getter and Setter==================================

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

}
